<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单 -->
      <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef">
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="用户名"
            prefix-icon="iconfont icon-yonghu1"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="密码"
            prefix-icon="iconfont icon-password-show"
            size="18px"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="login_button">
          <el-button type="primary" @click="loginForm">登录</el-button>
          <el-button type="info" @click="reactForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from "../assets/apis/login.js";
export default {
  data() {
    return {
      ruleForm: { username: "admin", password: "123456" },
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 6, max: 11, message: "长度在6 到 11 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    loginForm() {
      // 点击登录判断填写的账号密码是否符合规则
      this.$refs.ruleFormRef.validate(async valid => {
        if (valid) {
          const login_serve = await login("/login", this.ruleForm);
          console.log(login_serve);
          if (login_serve.meta.status == 200) {
            window.sessionStorage.setItem("token", login_serve.data.token);
            this.$router.push("/home");
            this.$message.success("登录成功");
          } else {
            this.$message.error("登录失败--账号或密码错误");
          }
        } else {
          this.$message.error("填写规则错误");
        }
      });
    },
    reactForm() {
      // 重置 清除input框输入的内容
      this.$refs.ruleFormRef.resetFields();
      //   console.log(this.$refs.ruleFormRef);
    }
  }
};
</script>
<style lang="less" scope>
.login_container {
  background: #2b4b6b;
  width: 100%;
  height: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      width: 80px;
      height: 80px;
      position: absolute;
      left: 50%;
      top: -10%;
      margin-left: -40px;
      border-radius: 50%;

      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #fff;
      }
    }
    .el-form {
      bottom: 0px;
      position: absolute;
      width: 100%;
      padding: 10px;
      box-sizing: border-box;
    }
    .login_button {
      display: flex;
      justify-content: flex-end;
    }
    .iconfont {
      font-size: 18px;
    }
  }
}
</style>